package net.geant.gembus.registry_publisher.mock;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.http.HttpConsumer;
import org.apache.camel.component.jetty.JettyHttpComponent;
import org.apache.camel.component.jetty.JettyHttpEndpoint;
import org.apache.camel.impl.DefaultCamelContext;

public class RestRegistryMock {
	
	public static void main(String[] args) throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addComponent("jetty", new JettyHttpComponent());
		
		// Create mock HTTP endpoint - listening on port 9081
		context.setStreamCaching(true);
		JettyHttpEndpoint jetty = context.getEndpoint("jetty:http://localhost:9081?matchOnUriPrefix=true", JettyHttpEndpoint.class);

		HttpConsumer c = (HttpConsumer) jetty.createConsumer(new Processor() {
			public void process(Exchange exchange) throws Exception {
				String body = exchange.getIn().getBody(String.class);
				System.out.println("Incoming HTTP request on URL: " + exchange.getIn().getHeader(Exchange.HTTP_URL));
				System.out.println("Path is: " + exchange.getIn().getHeader(Exchange.HTTP_PATH));
				System.out.println("HTTP Method is: " + exchange.getIn().getHeader(Exchange.HTTP_METHOD));
				System.out.println("Payload: " + body);
				System.out.println("Full headers: " + exchange.getIn().getHeaders());
				exchange.getOut().setBody("<ok />");
				exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
			}
		});
		c.start();
		context.start();
		System.out.println("Started Registry Mock listening on http://localhost:9081");
		
		Thread.sleep(100000000);

	}
}
